package com.feng.gaoerfu.activity;

import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.feng.gaoerfu.R;
import com.feng.gaoerfu.adapter.ContactAdapter;
import com.feng.gaoerfu.base.App;
import com.feng.gaoerfu.base.BaseActivity;
import com.feng.gaoerfu.bean.ContactPerson;
import com.feng.gaoerfu.utils.HanziToPinyin;
import com.feng.gaoerfu.view.SideBar;

import java.util.ArrayList;
import java.util.Collections;

import butterknife.InjectView;
import butterknife.OnClick;

/**
 * @time 2017/9/13 0013 21:55
 */

public class ContactPersonActivity extends BaseActivity {

    public static final String TAG = "ContactPersonActivity";
    @InjectView(R.id.tv_back)
    TextView mTvBack;
    @InjectView(R.id.tv_title)
    TextView mTvTitle;
    @InjectView(R.id.iv_menu)
    ImageView mIvMenu;
    @InjectView(R.id.contact_recycler)
    RecyclerView mContactRecycler;
    @InjectView(R.id.contact_sideBar)
    SideBar mContactSideBar;
    @InjectView(R.id.tv_first)
    TextView mTvFirst;
    private ContactAdapter mAdapter;

    @Override
    public int initContentView() {
        return R.layout.activity_contact_persons;
    }

    @Override
    public void initView() {
        mTvTitle.setText("联系人");
    }

    @Override
    public void initData() {
        if (App.mContactList == null) {
            App.mContactList = new ArrayList<>();
            initContact();
        }

        Collections.sort(App.mContactList);

        mContactRecycler.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        mAdapter = new ContactAdapter(R.layout.item_contact, App.mContactList);
        mContactRecycler.setAdapter(mAdapter);

        mAdapter.setOnRecyclerViewItemClickListener(new BaseQuickAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int i) {
                Intent intent = new Intent();
                intent.putExtra("name", App.mContactList.get(i).name);
                setResult(RESULT_OK, intent);
                finish();
            }
        });

    }

    private void initContact() {

        //得到ContentResolver对象
        ContentResolver cr = getContentResolver();
        //取得电话本中开始一项的光标
        Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        //向下移动光标
        while (cursor.moveToNext()) {

            ContactPerson contactPerson = new ContactPerson();
            //取得联系人名字
            int nameFieldColumnIndex = cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME);
            String name = cursor.getString(nameFieldColumnIndex);
            contactPerson.name = name;
            //取得电话号码
            String ContactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
            Cursor phone = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + ContactId, null, null);

            Log.d(TAG, "readContacts name = " + name);
            Log.d(TAG, "readContacts ContactId = " + ContactId);
            while (phone.moveToNext()) {
                String PhoneNumber = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                //格式化手机号
                PhoneNumber = PhoneNumber.replace("-", "");
                PhoneNumber = PhoneNumber.replace(" ", "");
                Log.d(TAG, "readContacts PhoneNumber = " + PhoneNumber);
                contactPerson.PhoneNumber = PhoneNumber;
                break;
            }
            contactPerson.setPinyin(HanziToPinyin.getPinYin(contactPerson.getName()));
            App.mContactList.add(contactPerson);
        }
    }


    @OnClick(R.id.tv_back)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void initListener() {
        super.initListener();
        mContactSideBar.setTextView(mTvFirst);
        mContactSideBar.setOnTouchingLetterChangedListener(new SideBar.OnTouchingLetterChangedListener() {
            @Override
            public void onTouchingLetterChanged(String s) {
                int position = mAdapter.getPositionForSection(s.charAt(0));

                if (position != -1) {
                    mContactRecycler.scrollToPosition(position);
                } else if (s.contains("#")) {
                    mContactRecycler.scrollToPosition(0);
                }
            }
        });
    }
}
